package com.gtq.uhome.controller;

import com.gtq.uhome.pojo.Consignee;
import com.gtq.uhome.result.Result;
import com.gtq.uhome.service.UserService;
import com.gtq.uhome.vo.UserInfo;
import com.gtq.uhome.vo.UserOrderInfor;
import com.sun.corba.se.impl.orbutil.ObjectStreamClassUtil_1_3;
import io.swagger.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.sound.midi.Soundbank;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ylhy
 * @Describe  总后台控制器
 *            模糊查询(用户名称,电话状态,站点)  请求路径/uhome/findUserByLike
 * @create 2020-07-30 16:04
 */
@Controller
@RequestMapping("/User")
@Api(value = "用于用户管理的相关操作",tags = "总后台-->用户管理的api-->饶平")
@ResponseBody
@CrossOrigin
public class UserController {

    @Resource(name = "userService")
    UserService userService;

    UserInfo userInfo = new UserInfo();

    /**
     * 查询所有用户 [姓名  电话  最后活跃时间  配送订单数  所属站点  状态  操作]
     * @return
     */
    @GetMapping(value = "/findAll")
    @ApiOperation(value = "查询所有用户信息",notes = "返回给前端显示[姓名  电话  最后活跃时间  配送订单数  所属站点  状态]",
            httpMethod = "GET")
    public Result findUser(){
        System.out.println("查询全部用户信息");
        List<UserInfo> userInfoList = null;

        try {
            userInfoList = userService.findAll();
        }catch (Exception e){
            return Result.failed("无符合条件的用户数据");
        }
        for (UserInfo info : userInfoList) {
            System.out.println(info.getUserName());
        }

        if (userInfoList!=null){
            return Result.ok(userInfoList,"查询所有用户信息成功");
        }else {
            return Result.failed("无用户数据");
        }
    }

    /**
     *  模糊查询用户 [姓名  电话  最后活跃时间  配送订单数  所属站点  状态  操作]冻结thaw  解冻thaw
     *  请求方式  post  携带参数  user_name  phone_Number status
     * @return
     */
    @PostMapping("/findByLike")
    @ApiOperation(value = "模糊查询",notes = "前端必须传  [关键字:keyWords   状态:freezeStatus  站点名称:SitName]",httpMethod = "POST")
    public Result findUserByLike(@RequestBody Map findByLike) {
        Object keyWord = findByLike.get("keyWords");
        Object freezeStatus =  findByLike.get("freezeStatus");
        Object SiteName = findByLike.get("SiteName");

        List<UserInfo> userInfoList = null;
        userInfoList = userService.findByLike(keyWord, freezeStatus, SiteName);

        for (UserInfo info : userInfoList) {
            System.out.println("++++++");
            info.getSiteName();
        }
        try {
             userInfoList = userService.findByLike(keyWord, freezeStatus, SiteName);
        }catch (Exception e){
            return Result.failed("无符合条件的用户数据");
        }

        for (UserInfo info : userInfoList) {
            System.out.println("用户信息"+info.getSiteName());
        }
        System.out.println(userInfoList.toArray().toString());
        System.out.println("_-----------------");
        if (userInfoList!=null){
            return Result.ok(userInfoList,"模糊查询成功");
        }else {
            return Result.failed("无符合条件的用户数据");
        }
    }


    @PostMapping("/freezeUser")
    @ApiOperation(value = "冻结用户",notes = "前端必须传选择中的用户的id---id数组",httpMethod = "POST")
    public Result freezeUser(@RequestBody Map map){

        System.out.println(map);

        ArrayList<Integer> ids = (ArrayList<Integer>) map.get("id");

        String freezeStatus = (String) map.get("freezeStatus");

        for (Integer id : ids) {
            System.out.println(id);
        }
        System.out.println(freezeStatus);

        Integer lines = null;
        try {
            lines = userService.freezeUser( ids, freezeStatus);
        }catch (Exception e){
            return Result.failed("操作失败,请检查是否选中");
        }
        if (lines < 0){
            return Result.failed("操作失败,请检重新选择");
        }
        else {
            return Result.ok(lines);
        }

    }

    @PostMapping("/thawUser")
    @ApiOperation(value = "解冻用户",notes = "前端必须传选择中的用户的id---id数组",httpMethod = "POST")
    public Result thawUser(@RequestBody Map map){
        ArrayList<Integer> ids = (ArrayList<Integer>) map.get("id");

        String freezeStatus = (String) map.get("freezeStatus");

        for (Integer id : ids) {
            System.out.println(id);
        }
        System.out.println(freezeStatus);

        Integer lines = null;
        try {
            lines = userService.freezeUser( ids, freezeStatus);
        }catch (Exception e){
            return Result.failed("操作失败,请检查是否选中");
        }
        if (lines < 0){
            return Result.failed("操作失败,请检重新选择");
        }
        else {
            return Result.ok(lines);
        }
    }





    @PostMapping("/findUserInfo")
    @ApiOperation(value = "根据用户id查询用户信息",notes = "前端必须传选择中的用户的id---单个用户的id",httpMethod = "POST")
    public Result<UserInfo> findUserInfo(@RequestBody Map map){
        Integer userId = (Integer) map.get("userId");
        System.out.println("findUserInfo : "+ userId);
        UserInfo userInfo = null;
        System.out.println(userId);
        try {
            userInfo = userService.findUserInfo(userId);
        }catch (Exception e){
            return Result.failed("操作失败,请检查是否选中");
        }

        if (userInfo!=null){
            return Result.ok(userInfo,"查询用户信息成功");
        }else {
            return Result.failed();
        }
    }

}
